package ecoll.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

import ecoll.shared.Constant;

public class LoginPanel extends DockPanel {

	private Label loginLabel = new Label();
	private Label nickLabel = new Label();
	private HorizontalPanel buttonPanel = new HorizontalPanel();
	private Image googleIcon = new Image("images/" + Constant.GOOGLE_ICON);
	private Button loginButton = new Button();

	public LoginPanel(boolean horizontal) {
		add(loginLabel, (horizontal)?DockPanel.WEST:DockPanel.NORTH);
		add(nickLabel, (horizontal)?DockPanel.WEST:DockPanel.NORTH);
		googleIcon.setStyleName("googleIcon");
		
		buttonPanel.add(googleIcon);
		buttonPanel.add(loginButton);
		buttonPanel.setStyleName("button");
		add(buttonPanel, (horizontal)?DockPanel.EAST:DockPanel.SOUTH);
		setCellVerticalAlignment(loginLabel, HasVerticalAlignment.ALIGN_MIDDLE);
	}

	public void enableLogin(final String loginUrl) {
		loginButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Window.Location.assign(loginUrl);
			}
		});
		loginButton.setText("Sign in");
		loginLabel.setText("Please sign in to your Google Account.");
	}

	public void enableLogout(final String nick, final String logoutUrl) {
		loginButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Window.Location.assign(logoutUrl);
			}
		});
		loginButton.setText("Sign out");
		loginLabel.setText("Logged as " + nick);
	}
}
